package com.htfyun.ft.pccommand;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;


import com.htfyun.ft.MainActivity;
import com.htfyun.ft.R;
import com.htfyun.ft.agingtest.AgingBooleanSettingsAttr;
import com.htfyun.ft.agingtest.AgingTestItemAttr;
import com.htfyun.ft.common.FTUtils;
import com.htfyun.ft.common.PermissionManager;
import com.htfyun.ft.config.NVRomKeysAttr;
import com.htfyun.ft.fragment.factory.FactoryTestItemsEnum;
import com.htfyun.ft.fragment.factory.ResultAttr;
import com.htfyun.hardware.NVRomController;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
 * Created by sst on 2018/7/25.
 */

public class PCCommandHandleActivity extends AppCompatActivity {

    private final static String TAG = PCCommandReceiver.TAG;

    private Activity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pc_command_activity);

        mActivity = this;

        initPermission();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "pc command destroy");
    }


    private void initPermission() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            //同时申请多个权限
            PermissionManager.getInstance(getApplicationContext()).execute(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE

            );

        } else {

            tryHandleAction();

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.getInstance(getApplicationContext()).
                onRequestPermissionsResult(requestCode, permissions, grantResults);

        Log.e(TAG, "pc command ~~~~~~~~~~~~~~onRequestPermissionsResult");

        tryHandleAction();

    }

    private void tryHandleAction() {

        Intent intent = getIntent();
        if (null == intent) {
            ActivityCompat.finishAfterTransition(this);
            return;
        }
        String action = intent.getAction();
        handleAction(this, action, intent);

        ActivityCompat.finishAfterTransition(this);

    }

    private void handleAction(Context context, String action, Intent intent) {

        if (action == null) {
            return;
        }

        if (action.equals(PCCommand.PC_ACTION_CLEAR_EMMC_FAILED_FLAG)) {
            AgingBooleanSettingsAttr.emmc_fail.set(false);
        } else if (action.equals(PCCommand.PC_ACTION_CLEAR_CPU_FAILED_FLAG)) {
            AgingBooleanSettingsAttr.cpu_fail.set(false);
        } else if (action.equals(PCCommand.PC_ACTION_SET_AGING_ITEM_FORCE_TEST_OVER)) {
            final int id = intent.getIntExtra(PCCommand.PC_ACTION_AGING_ITEM_INT_EXTRA, -1);
            for (AgingTestItemAttr itemAttr : AgingTestItemAttr.values()) {
                if (id == itemAttr.getId()) {
                    itemAttr.setForceTestOver(true);
                    break;
                }
            }
        } else if (action.equals(PCCommand.PC_ACTION_CLR_AGING_ITEM_FORCE_TEST_OVER)) {
            final int id = intent.getIntExtra(PCCommand.PC_ACTION_AGING_ITEM_INT_EXTRA, -1);
            for (AgingTestItemAttr itemAttr : AgingTestItemAttr.values()) {
                if (id == itemAttr.getId()) {
                    itemAttr.setForceTestOver(false);
                    break;
                }
            }
        } else if (action.equals(PCCommand.PC_ACTION_SET_AGING_DISABLED)) {
            AgingBooleanSettingsAttr.aging_enabled.set(false);
        } else if (action.equals(PCCommand.PC_ACTION_SET_ALL_PASSED)) {
            for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
                itemsEnum.setResult(ResultAttr.PASS);
            }
        } else if (action.equals(PCCommand.PC_ACTION_SET_ALL_FAILED)) {
            for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
                itemsEnum.setResult(ResultAttr.FAIL);
            }
        } else if (action.equals(PCCommand.PC_ACTION_SET_ITEM_PASSED)) {
            final int id = intent.getIntExtra(PCCommand.PC_ACTION_SET_ITEM_INT_EXTRA, -1);
            for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
                if (id == itemsEnum.ordinal()) {
                    itemsEnum.setResult(ResultAttr.PASS);
                    break;
                }
            }
        } else if (action.equals(PCCommand.PC_ACTION_SET_ITEM_FAILED)) {
            final int id = intent.getIntExtra(PCCommand.PC_ACTION_SET_ITEM_INT_EXTRA, -1);
            for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
                if (id == itemsEnum.ordinal()) {
                    itemsEnum.setResult(ResultAttr.FAIL);
                    break;
                }
            }
        } else if (action.equals(PCCommand.PC_ACTION_FORCE_CHECKED)) {
            for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
                itemsEnum.setResult(ResultAttr.PASS);
            }
            NVRomController.getInstance().setNVRomString(NVRomKeysAttr.pc_checked.getKey(), NVRomKeysAttr.PC_CHECKED);
            FTUtils.stopFTFloat();
            FTUtils.startDefaultHome();

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    android.os.Process.killProcess(android.os.Process.myPid());

                }
            }, 500);
        }

    }

    private void startMainActivity(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}
